#coding=utf-8
import requests,json,urllib
import mysql_password
import id_card
import hashlib
import random
#注册用户
def register(phone,pwd,username):
    try:
        s = requests.session()
        #获取加密后的密码、code、salt
        get_code = s.get('http://community-platform-api-test.gdgyun.com/api/community/account/get/verificationCode?mobile=' + phone)
        code = str(get_code.json()["data"]["code"])
        salt = str(get_code.json()["data"]["salt"])
        m = hashlib.md5()  # 创建md5对象
        m.update(pwd)  # 生成加密串，其中password是要加密的字符串,第一次加密
        pwd1 = m.hexdigest()  # 打印经过md5加密的字符串
        pwd = hashlib.md5()  # 第二次加密
        pwd.update(str(pwd1) + salt)
        list = [pwd.hexdigest(), code, salt]
        print list[0], list[1], list[2]
        #通过id_card函数获取身份证号码
        idcard = id_card.idcard()
        register_data = ({"mobile":str(phone),"encryptedPassword":list[0],"code":list[1]})
        register = s.post('http://community-platform-api-test.gdgyun.com/api/community/account/register',json=register_data)
        authenticate_data = ({"realName":str(username),"idCardNo":idcard,"mobile":str(phone)})
        authenticate = s.post('http://community-platform-api-test.gdgyun.com/api/community/account/authenticate',json=authenticate_data)
        address = str(authenticate.json()["data"]["address"])
        birthday = str(authenticate.json()["data"]["birthday"])
        activate_data = ({"authCode":"38ZG4FSR","address":address,"birthday":birthday,"encryptedPassword":list[0],"gender":"FEMALE","idCardNo":idcard,"mobile":str(phone),"realName":str(username),"salt":list[2]})
        activate = s.post('http://community-platform-api-test.gdgyun.com/api/community/account/activate',json=activate_data)
        print register.text
        print authenticate.text
        print activate.text
    except:
        print u'异常！'
#随机生成中文姓名
def name():
    last_names = ['赵', '钱', '孙', '李', '周', '吴', '郑', '王', '冯', '陈', '褚', '卫', '蒋', '沈', '韩', '杨', '朱', '秦', '尤', '许',
                  '姚', '邵', '堪', '汪', '祁', '毛', '禹', '狄', '米', '贝', '明', '臧', '计', '伏', '成', '戴', '谈', '宋', '茅', '庞',
                  '熊', '纪', '舒', '屈', '项', '祝', '董', '梁']

    first_names = ['的', '一', '是', '了', '我', '不', '人', '在', '他', '有', '这', '个', '上', '们', '来', '到', '时', '大', '地', '为',
                   '子', '中', '你', '说', '生', '国', '年', '着', '就', '那', '和', '要', '她', '出', '也', '得', '里', '后', '自', '以',
                   '乾', '坤', '舒', '屈', '项', '祝', '董', '梁']
    num1 = random.randint(0,47)
    num2 = random.randint(0,47)
    name = last_names[num1] + first_names[num2]
    print name
    return name

if __name__ =='__main__':
   for i in range(1,20001200,1):
        register('1770003000'+str(i),'cq123321',str(name()))

